# Create your views here.
from stw_app import models
from stw_app.models import UserInfo
from django.shortcuts import render, redirect
from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt
import re

@csrf_exempt
def get_details(request):
    if request.method == "POST":
        order_id = request.POST.get('order_id')
        order = models.Order.objects.get(id=order_id)
        # print(order)
        # result_string = re.sub('[a-zA-Z]', ' ', order)
        # print(result_string)
        date_string = order.start_time.strftime('%Y-%m-%d %H:%M:%S')
        print(date_string)
        data = {
            'title': order.title,
            'customer_id': order.customer_id,
            'duration': order.duration,
            'type': order.type,
            'description': order.description,
            'release_time': order.release_time,
            'date': order.date,
            'start_time': date_string,
            'end_time': order.end_time,
            'latitude': order.latitude,
            'longitude': order.longitude,
            'gender_requirement': order.gender_requirement,
            'requirement': order.requirement,
            'demand':order.demand,
        }
        return JsonResponse({'success': True, 'data': data})
    return JsonResponse({'success': False})


@csrf_exempt
def get_customer(request):
    if request.method == "POST":
        customer_id = request.POST.get('customer_id')
        customer_id = int(customer_id)
        customer = models.UserInfo.objects.get(id=customer_id)
        print(customer)
        data = {
            'nick_name': customer.nick_name,
            'age': customer.age,
            'gender': customer.gender,
            'belief_score': customer.belief_score
        }
        return JsonResponse({'success': True, 'data': data})
    return JsonResponse({'success': False})
